const util = require('./util.js')
const baseurl = util.baseurl

export { getVolunteerList, getVolunteerDetail, editVolunteer, addVolunteer };

// 获取志愿者列表
const getVolunteerList = () => {
    return new Promise((resolve, reject) => {
        const userInfo = wx.getStorageSync('userInfo')
        // console.log(userInfo)
        wx.request({
            url: baseurl + '/ManageVolunteer/GetVolunteerList',
            method: 'GET',
            header: {
                'content-type': 'application/x-www-form-urlencoded',
                'Username': userInfo.username,
                'Authorization': userInfo.token,
                'Userrole': userInfo.role,
                'Uid': userInfo.userid
            },
            success: (res) => {
                resolve(res.data)
            },
            fail: (err) => {
                reject(err)
            }
        })
    })
}
//获取志愿者详情
const getVolunteerDetail = (uid) => {
    return new Promise((resolve, reject) => {
        const userInfo = wx.getStorageSync('userInfo')
        // console.log(userInfo)
        wx.request({
            url: baseurl + '/Volunteer/GetVolunteerDetail',
            method: 'GET',
            data: {
                'id': uid
            },
            header: {
                'content-type': 'application/x-www-form-urlencoded',
                'Username': userInfo.username,
                'Authorization': userInfo.token,
                'Userrole': userInfo.role,
                'Uid': userInfo.userid
            },
            success: (res) => {
                resolve(res.data)
            },
            fail: (err) => {
                reject(err)
            }
        })
    })
}
// 修改志愿者
const editVolunteer = (data) => {
    return new Promise((resolve, reject) => {
        const userInfo = wx.getStorageSync('userInfo')
        // console.log(userInfo)
        wx.request({
            url: baseurl + '/Volunteer/UpdateVolunteer',
            method: 'POST',
            data: {
                'Uid': data.id,
                'Name': data.name,
                'Phone': data.phone,
                'IdentificationNumber': data.idNumber,
                'Address': data.homeAddress,
                'Skill': data.skills,
                'Standby': data.email,
                'Gender': data.gender,
                'BirthDate': data.birthDate,
            },
            header: {
                'content-type': 'application/json',
                'Username': userInfo.username,
                'Authorization': userInfo.token,
                'Userrole': userInfo.role,
                'Uid': userInfo.userid
            },
            success: (res) => {
                resolve(res.data)
            },
            fail: (err) => {
                reject(err)
            }
        })
    })
}  
// 添加志愿者
const addVolunteer = (data) => {
    return new Promise((resolve, reject) => {
        const userInfo = wx.getStorageSync('userInfo')
        // console.log(userInfo)
        wx.request({
            url: baseurl + '/Volunteer/EnrollVolunteer',
            method: 'POST',
            data: {
                'Uid': data.id,
                'Name': data.name,
                'Phone': data.phone,
                'IdentificationNumber': data.idNumber,
                'Address': data.homeAddress,
                'Skill': data.skills,
                'Standby': data.email,
                'Gender': data.gender,
                'BirthDate': data.birthDate,
                'Role': 'Volunteer',
            },
            header: {
                'content-type': 'application/json',
                'Username': userInfo.username,
                'Authorization': userInfo.token,
                'Userrole': userInfo.role,
                'Uid': userInfo.userid
            },
            success: (res) => {
                resolve(res.data)
            },
            fail: (err) => {
                reject(err)
            }
        })
    })
}  
